<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item v-if="hasUserQueryAuth" label="用户ID" prop="userId">
        <UserSelect v-model="queryParams.userId" clearable/>
      </el-form-item>
      <el-form-item label="类别ID" prop="categoryId">
        <AccountCatTreeSelect v-model="queryParams.categoryId"/>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
            v-model="queryParams.amount"
            placeholder="请输入金额"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间" prop="transactionTime">
        <el-date-picker clearable
                        v-model="queryParams.transactionTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择交易时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地点" prop="location">
        <el-input
            v-model="queryParams.location"
            placeholder="请输入地点"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-input
            v-model="queryParams.tags"
            placeholder="请输入标签"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['easybook:accountList:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['easybook:accountList:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['easybook:accountList:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['easybook:accountList:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="accountListList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="记账记录ID" align="center" prop="accountId"/>
      <IconColumn :show-id="false"/>
      <el-table-column label="用户ID" align="center" prop="userId"></el-table-column>
      <el-table-column label="用户名" align="center" prop="userName"></el-table-column>
      <el-table-column label="类别ID" align="center" prop="categoryId"></el-table-column>
      <el-table-column label="类别" align="center" prop="categoryName"></el-table-column>
      <el-table-column label="金额" align="center" prop="amount"/>
      <el-table-column label="交易时间" align="center" prop="transactionTime" width="180">
        <template #default="scope">
          <span>{{
              parseTime(scope.row.transactionTime,
                  '{y}-{m}-{d}')
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="note"/>
      <el-table-column label="地点" align="center" prop="location"/>
      <el-table-column label="标签" align="center" prop="tags"/>
      <CreateColumn/>
      <el-table-column label="附件ID" align="center" prop="attachmentIds">
        <template #default="scope">
          <eb-image-preview :ids="scope.row.attachmentIds"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status"></el-table-column>
      <el-table-column fixed="right" width="150" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['easybook:accountList:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['easybook:accountList:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <!-- 添加或修改记账列表对话框 -->
    <el-dialog destroy-on-close :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="accountListRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="hasUserQueryAuth" label="用户ID" prop="userId">
          <UserSelect v-model="form.userId" clearable/>
        </el-form-item>
        <el-form-item label="类别ID" prop="categoryId">
          <AccountCatTreeSelect v-model="form.categoryId"/>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="交易时间" prop="transactionTime">
          <el-date-picker clearable
                          v-model="form.transactionTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择交易时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="地点" prop="location">
          <el-input v-model="form.location" placeholder="请输入地点"/>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入标签"/>
        </el-form-item>
        <el-form-item label="附件ID" prop="attachmentIds">
          <EbMultiImageUpload :upload-img-url="uploadImgUrl" v-model="form.attachmentIds"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="AccountList">
import {
  addAccountList,
  delAccountList,
  getAccountList,
  listAccountList,
  updateAccountList
} from "@/api/easybook/accountList";
import UserSelect from "@/components/EbComponent/Selector/UserSelect/UserSelect.vue";
import auth from '@/plugins/auth'
import CreateColumn from "@/components/EbComponent/TableColumn/CreateColumn.vue";
import AccountCatTreeSelect from "@/components/EbComponent/Selector/AccountCatSelect/AccountCatTreeSelect.vue";
import EbIconUpload from "@/components/EbComponent/ImageUpload/EbIconUpload.vue";
import IconColumn from "@/components/EbComponent/TableColumn/IconColumn.vue";
const hasUserQueryAuth = ref(auth.hasPermi('system:user:query'));
const {proxy} = getCurrentInstance();
const accountListList = ref([]);
const uploadImgUrl = ref(import.meta.env.VITE_APP_BASE_API + "/easybook/attachments/upload");

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    categoryId: null,
    amount: null,
    transactionTime: null,
    note: null,
    location: null,
    tags: null,
    status: null
  },
  rules: {
    amount: [
      {
        required: true,
        message: "金额不能为空",
        trigger: "blur"
      }
    ],
  }
});
const {
  queryParams,
  form,
  rules
} = toRefs(data);
/** 查询记账列表列表 */
function getList() {
  loading.value = true;
  listAccountList(queryParams.value)
      .then(response => {
        accountListList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    accountId: null,
    userId: null,
    categoryId: null,
    amount: null,
    transactionTime: null,
    note: null,
    location: null,
    tags: null,
    attachmentIds: null,
    status: null
  };
  proxy.resetForm("accountListRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.accountId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加记账列表";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _accountId = row.accountId || ids.value
  getAccountList(_accountId)
      .then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改记账列表";
      });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["accountListRef"].validate(valid => {
    if (valid) {
      if (form.value.accountId != null) {
        updateAccountList(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("修改成功");
              open.value = false;
              getList();
            });
      } else {
        addAccountList(form.value)
            .then(response => {
              proxy.$modal.msgSuccess("新增成功");
              open.value = false;
              getList();
            });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _accountIds = row.accountId || ids.value;
  proxy.$modal.confirm('是否确认删除记账列表编号为"' + _accountIds + '"的数据项？')
      .then(function () {
        return delAccountList(_accountIds);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      })
      .catch(() => {
      });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download('easybook/accountList/export',
      {
        ...queryParams.value
      },
      `accountList_${new Date().getTime()}.xlsx`)
}
getList();
</script>
